package com.tjsoft.system.web.account;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.tjsoft.system.entity.base.User;
import com.tjsoft.system.service.AccountManager;
import com.tjsoft.system.util.MD5;
import com.tjsoft.system.web.CrudActionSupport;

//namespace与application下的目录对应dispatcher
@Namespace("/app/sys/user")
@Results( { @Result(name = "reload", location = "/app/sys/user/user!list", type = "redirect"),
	@Result(name = "index", location = "/index.jsp", type = "dispatcher"),
	@Result(name = "main", location = "/app/main2/index.jsp", type = "dispatcher")})
public class UserAction extends CrudActionSupport<User> {

	private static final long serialVersionUID = 8683878162525847072L;

	@Autowired
	private AccountManager accountManager;

	private Long id ;
	private User entity;
	private Page<User> page = new Page<User>(10);
	private Long[] chkDel; 
	private String errMsg = "";
	private String newpwd;
	
	public void validateSave(){
		try {
			String hql = "";
			long count = 0;
			if(id==null){
				hql = "select count(*) from User u where u.loginName=?";
				count = accountManager.getUserDAO().findUnique(hql, entity.getLoginName());
			}else{
				hql = "select count(*) from User u where u.loginName=? and u.id != ?";
				count = accountManager.getUserDAO().findUnique(hql, entity.getLoginName(),id);
			}
			
			if("".equals(entity.getLoginName())){
				this.addFieldError("msg.loginName", "登录名不能为空");
			}else if(count>0){
				this.addFieldError("msg.loginName", "登录名重复");
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	public String changepwd(){
		try {
			User u = (User)Struts2Utils.getSession().getAttribute("user");
			if(!u.getPassword().equals(new MD5().getMD5ofStr(entity.getPassword()))){
				addActionMessage("当前密码输入错误");
			}else{
				u.setPassword(new MD5().getMD5ofStr(newpwd));
				accountManager.saveUser(u);
				addActionMessage("密码修改成功");
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionMessage("修改密码失败");
		}
		return "changepwd";
	}
	
	public String login(){
		String result = "";
		try {
			String hql = "from User u where u.loginName=? and u.password=? and u.status='Y'";
			List<User> users = accountManager.getUserDAO().find(hql,entity.getLoginName(),entity.getMD5Pwd());
			if(users.size()==1){
				//成功
				result = "ok";
				Struts2Utils.getSession().setAttribute("user", users.get(0));
			}else{
				errMsg = "登录失败！用户名或密码错误！";
				result = "fail";
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionMessage("获取登录");
		}
		if("ok".equals(result)){
			return "main";
		}else{
			return "index";
		}
	}
	
	@Override
	public String delete()  {
		try {
			if(chkDel!=null && chkDel.length>0){
				for(Long id:chkDel){
					accountManager.deleteUser(id);
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除用户失败");
		}
		return RELOAD;
	}
	@Override
	public String input()  {
		return INPUT;
	}
	
	@Override
	public String list() {
		try {
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
			page = accountManager.getUserDAO().findPage(page, filters);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("加载用户失败");
		}
		return "list";
	}
	@Override
	protected void prepareModel()  {
		if (id != null) {
			try {
				entity = accountManager.getUserDAO().get(id);
			} catch (Exception e) {
				logger.error(e.getMessage(),e);
				addActionMessage("加载用户失败");
			}
		} else {
			entity = new User();
		}
	}
	@Override
	public String save()  {
		try {
			if("".equals(entity.getShowName())){
				entity.setShowName(entity.getLoginName());
			}
			accountManager.saveUser(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("保存用户失败");
		}
		return RELOAD;
	}
	
	public User getModel() {
		return entity;
	}
	
	//＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝
	public AccountManager getAccountManager() {
		return accountManager;
	}
	
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
	public Page<User> getPage() {
		return page;
	}
	public void setPage(Page<User> page) {
		this.page = page;
	}
	public User getEntity() {
		return entity;
	}
	public void setEntity(User entity) {
		this.entity = entity;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	public String getErrMsg() {
		return errMsg;
	}

	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	public Long[] getChkDel() {
		return chkDel;
	}

	public void setChkDel(Long[] chkDel) {
		this.chkDel = chkDel;
	}

	public String getNewpwd() {
		return newpwd;
	}

	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}
}
